// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
    const db = cloud.database();
    const _ = db.command;
    const now = new Date(new Date().getTime() + 28800000);
    var year = now.getFullYear(),
        month = `${now.getMonth() + 1}`,
        date = `${now.getDate()}`,
        hour = `${now.getHours()}`,
        minute = `${now.getMinutes()}`;
    if (month.length === 1) month = '0' + month;
    if (date.length === 1) date = '0' + date;
    if (hour.length === 1) hour = '0' + hour;
    if (minute.length === 1) minute = '0' + minute;
    const nowDate = `${year}-${month}-${date}`;
    const nowTime = `${hour}:${minute}`;
    const UOR_orderID = (await db.collection('UOR').where({
        repairmanID: '',
        finished: false
    }).get()).data.map(item => {
        return item.orderID
    });
    return (await db.collection('repairOrder').orderBy('date', 'asc').where(_.or([
        { _id: _.in(UOR_orderID), date: _.gt(nowDate) },
        { _id: _.in(UOR_orderID), date: _.eq(nowDate), time: _.gt(nowTime) }
    ])).get()).data;
}